﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using BLL.Theme;
using System.Diagnostics;
using BLL.Formation;
using BLL.SessionFormation;
using BLL.Formateur;

namespace UI
{
    public partial class ManageCourses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DisplayFormations();
                DisplayFormationsPlanned();
            }
        }

        protected void AddFormation_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("AddFormation.aspx");
        }

        protected void PlanAFormation_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("AddFormationPlanifie.aspx");           
        }

        

        /*****************************
         * Formations Methods
         *****************************/

        private void DisplayFormations()
        {
            this.GridViewFormations.DataSource = null;

            List<FormationBO> formations = FormationBLL.GetFormations();

            if (formations.Count > 0)
            {
                GridViewFormations.DataSource = formations;
                GridViewFormations.DataBind();
            }
        }

        protected void GridViewFormations_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (this.GridViewFormations.EditIndex == e.Row.RowIndex && GridViewFormations.EditIndex != -1)
            //if (e.Row.RowState == DataControlRowState.Edit )
            {
                (e.Row.FindControl("ddlTheme") as DropDownList).DataSource = ThemeBLL.GetThemes();
                (e.Row.FindControl("ddlTheme") as DropDownList).DataBind();
            }
        }

        protected void GridViewFormations_OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridViewFormations.EditIndex = e.NewEditIndex;
            //this.GridViewFormations.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;

            DisplayFormations();
        }

        protected void GridViewFormations_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                FormationBO bo = RetrieveEditValuesForFormation(e.RowIndex);

                FormationBLL.UpdateFormation(bo);

                GridViewFormations.EditIndex = -1;

                DisplayFormations();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        protected void GridViewFormations_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                FormationBO bo = new FormationBO();
                bo.Identifiant = int.Parse((GridViewFormations.Rows[e.RowIndex].FindControl("lblIdentifiant") as Label).Text);

                FormationBLL.DeleteFormation(bo);

                //this.GridViewFormations.Rows[e.RowIndex].RowState = DataControlRowState.Normal;

                GridViewFormations.EditIndex = -1;

                DisplayFormations();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        protected void GridViewFormations_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewFormations.Rows[e.RowIndex].RowState = DataControlRowState.Normal;

            GridViewFormations.EditIndex = -1;

            DisplayFormations();
        }

        private FormationBO RetrieveEditValuesForFormation(int index)
        {
            FormationBO bo = new FormationBO();

            try
            {
                bo.Identifiant = int.Parse((GridViewFormations.Rows[index].FindControl("lblIdentifiantEditView") as Label).Text);
                bo.Libelle = (GridViewFormations.Rows[index].FindControl("tbLibelle") as TextBox).Text;
                bo.NombreJours = int.Parse((GridViewFormations.Rows[index].FindControl("tbNombreJours") as TextBox).Text);
                bo.Prix = double.Parse((GridViewFormations.Rows[index].FindControl("tbPrix") as TextBox).Text);
                bo.Reference = (GridViewFormations.Rows[index].FindControl("tbReference") as TextBox).Text;
                bo.Description = (GridViewFormations.Rows[index].FindControl("tbDescription") as TextBox).Text;

                ThemeBO themeSelected = new ThemeBO();
                themeSelected.Identifiant = int.Parse((GridViewFormations.Rows[index].FindControl("ddlTheme") as DropDownList).SelectedValue);
                themeSelected.Libelle = (GridViewFormations.Rows[index].FindControl("ddlTheme") as DropDownList).SelectedItem.Text;
                bo.Theme = themeSelected;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        /*****************************
        * Session Formations Methods
        *****************************/

        private void DisplayFormationsPlanned()
        {
            this.GridViewFormationsPlanned.DataSource = null;

            List<SessionFormationBO> sessionFormations = SessionFormationBLL.GetSessionFormations();

            if (sessionFormations.Count > 0)
            {
                GridViewFormationsPlanned.DataSource = sessionFormations;
                GridViewFormationsPlanned.DataBind();
            }
        }

        protected void GridViewFormationsPlanned_OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridViewFormationsPlanned.EditIndex = e.NewEditIndex;
            //this.GridViewFormationsPlanned.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;

            DisplayFormationsPlanned();
        }

        protected void GridViewFormationsPlanned_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (this.GridViewFormationsPlanned.EditIndex == e.Row.RowIndex && GridViewFormationsPlanned.EditIndex != -1)
            {
                (e.Row.FindControl("ddlFormateurs") as DropDownList).DataSource = FormateurBLL.GetFomateurs();
                (e.Row.FindControl("ddlFormateurs") as DropDownList).DataBind();

                (e.Row.FindControl("ddlFormations") as DropDownList).DataSource = FormationBLL.GetFormations();
                (e.Row.FindControl("ddlFormations") as DropDownList).DataBind();
            }
        }

        protected void GridViewFormationsPlanned_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                SessionFormationBO bo = RetrieveEditValuesForSessionFormations(e.RowIndex);

                SessionFormationBLL.UpdateSessionFormations(bo);

                GridViewFormationsPlanned.EditIndex = -1;

                DisplayFormationsPlanned();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        protected void GridViewFormationsPlanned_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {   
                int id = int.Parse((GridViewFormationsPlanned.Rows[e.RowIndex].FindControl("lblIdentifiant") as Label).Text);

                SessionFormationBLL.DeleteSessionFormations(id);

                GridViewFormationsPlanned.EditIndex = -1;

                DisplayFormationsPlanned();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        protected void GridViewFormationsPlanned_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewFormationsPlanned.Rows[e.RowIndex].RowState = DataControlRowState.Normal;

            GridViewFormationsPlanned.EditIndex = -1;

            DisplayFormationsPlanned();
        }

        private SessionFormationBO RetrieveEditValuesForSessionFormations(int index)
        {
            SessionFormationBO bo = new SessionFormationBO();

            try
            {
                bo.Identifiant = int.Parse((GridViewFormationsPlanned.Rows[index].FindControl("lblIdentifiantEditView") as Label).Text);
                bo.DateDebut = DateTime.Parse((GridViewFormationsPlanned.Rows[index].FindControl("tbDateDebut") as TextBox).Text);
                bo.DateFin = DateTime.Parse((GridViewFormationsPlanned.Rows[index].FindControl("tbDateFin") as TextBox).Text);
                int FormateurId = int.Parse((GridViewFormationsPlanned.Rows[index].FindControl("ddlFormateurs") as DropDownList).SelectedValue);
                int FormationId = int.Parse((GridViewFormationsPlanned.Rows[index].FindControl("ddlFormations") as DropDownList).SelectedValue);
                bo.Formateur = FormateurBLL.GetFomateurs().FirstOrDefault(f => f.Identifiant == FormateurId);
                bo.Formation = FormationBLL.GetFormations().FirstOrDefault(f => f.Identifiant == FormationId);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }
    }
}